import Vue from 'vue'
import VueRouter from 'vue-router'
//引入首页路由
// eslint-disable-next-line no-unused-vars
import Movie from '../views/home/Movie.vue'

Vue.use(VueRouter)

const routes = [
  //重定向
  {
    path: '/',
    redirect: '/movie'
  },
  {
    path: '/movie',//根目录
    name: 'movie',//路径名称
    component: Movie,
    //定子路由
    children: [
      {
        path: '/movie',
        redirect: '/movie/reying'
      },

      {
        path: 'reying',
        name: 'reying',
        component: () => import(/* webpackChunkName: "reying" */ '../views/home/Reying.vue')
      },
      {
        path: 'daiying',
        name: 'daiying',
        component: () => import(/* webpackChunkName: "daiying" */ '../views/home/Daiying.vue')
      },
      {
        path: 'jingdian',
        name: 'jingdian',
        component: () => import(/* webpackChunkName: "jingdian" */ '../views/home/Jingdian.vue')
      },
      {
        path: 'yingyuan',
        name: 'yingyuan',
        component: () => import(/* webpackChunkName: "yingyuan" */ '../views/home/Yingyuan.vue')
      },
    ]
  },
  {
    path: '/video',
    name: 'video',
    component: () => import(/* webpackChunkName: "video" */ '../views/Video.vue')
  },
  {
    path: '/shortvideo',
    name: 'shortvideo',
    component: () => import(/* webpackChunkName: "shortvideo" */ '../views/ShortVideo.vue')
  },
  {
    path: '/show',
    name: 'show',
    component: () => import(/* webpackChunkName: "show" */ '../views/Show.vue')
  },
  {
    path: '/me',
    name: 'me',
    component: () => import(/* webpackChunkName: "me" */ '../views/Me.vue')
  },
  {
    path: '/movieitem/:id',
    name: 'movieitem',
    component: () => import(/* webpackChunkName: "movieitem" */ '../views/MovieItem.vue')
  },
  {
    path: '/citylist',
    name: 'movieitem',
    component: () => import(/* webpackChunkName: "citylist" */ '../views/CityList.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    path: '*',
    name: 'page404',
    component: () => import(/* webpackChunkName: "page404" */ '../views/Page404.vue')
  },
]

const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    console.log(to, from, savedPosition);
    return { y: 0 }
  }
})
//路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to, from, next);
  //判断需要守卫的页面
  if (to.path == '/me') {//需要守卫的页面
    if (localStorage.getItem('user')) {//登录后允许进入
      next();
    } else {//跳转到登录页
      next('/login');
    }
  } else {//其他页面放行
    //放行
    next();
  }

})

export default router